# -*- coding: UTF-8 -*-
"""
@Project ：swift-forge-engine 
@File    ：opera_log.py
@IDE     ：PyCharm 
@Author  ：Tony.Gan
@Date    ：2025/2/22 22:23 
@Des     ：
"""
from typing import Annotated

from fastapi import APIRouter, Depends, Query, Request
from fastapi_limiter.depends import RateLimiter

from App.app.admin.schemas.opera_log_schema import GetOperaLogListDetails
from App.app.admin.services.orera_log_service import OperaLogService
from App.common.pagination import DependsPagination, paging_data
from App.common.respones.response_schema import ResponseModel, response_base
from App.common.security.jwt import DependsJwtAuth
from App.database.mysql import CurrentSession

router = APIRouter(prefix='/opera')


@router.get(
    '',
    summary='（模糊条件）分页获取操作日志',
    description='通过条件筛选和分页查询所有操作日志',
    dependencies=[
        Depends(RateLimiter(times=20, minutes=1)),
        DependsJwtAuth,
        DependsPagination,
    ],
)
async def get_pagination_opera_logs(
        db: CurrentSession,
        username: Annotated[str | None, Query()] = None,
        status: Annotated[str | None, Query()] = None,
        ip: Annotated[str | None, Query()] = None,
        sort_field: Annotated[str | None, Query()] = None,
        sort_order: Annotated[str | None, Query()] = None
) -> ResponseModel:
    log_select = await OperaLogService.get_select(username=username, status=status, ip=ip, sort_field=sort_field,
                                                  sort_order=sort_order)
    page_data = await paging_data(db, log_select, GetOperaLogListDetails)
    return response_base.success(data=page_data)


@router.delete(
    '',
    summary='（批量）删除操作日志',
    description='（批量）删除操作日志',
    dependencies=[Depends(RateLimiter(times=20, minutes=1)), DependsJwtAuth]
)
async def delete_opera_log(request: Request, pk: Annotated[list[int], Query(...)]) -> ResponseModel:
    count = await OperaLogService.delete(request=request, pk=pk)
    if count > 0:
        return response_base.success()
    return response_base.fail()


@router.delete(
    '/all',
    summary='清空操作日志',
    description='清空操作日志',
    dependencies=[Depends(RateLimiter(times=20, minutes=1)), DependsJwtAuth]
)
async def delete_all_opera_logs(request: Request) -> ResponseModel:
    count = await OperaLogService.delete_all(request)
    if count > 0:
        return response_base.success()
    return response_base.fail()
